﻿//using Android.Views;
using Microsoft.Maui.Controls;

namespace CalculatorApp
{
    public partial class MainPage : ContentPage
    {
        private double _currentNumber = 0;
        private string _currentOperation = "";
        private bool _isFirstNumber = true;

        public MainPage()
        {
            InitializeComponent();
            Resources = new ResourceDictionary {
                { "ButtonStyle", new Style(typeof(Button)) {
                    Setters = {
                        new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromRgb(255, 255, 255) },
                        new Setter { Property = Button.TextColorProperty, Value = Colors.Black },
                        new Setter { Property = Button.CornerRadiusProperty, Value = 5 },
                        new Setter { Property = Button.FontAttributesProperty, Value = FontAttributes.Bold },
                        new Setter { Property = Button.FontSizeProperty, Value = 18 },
                        new Setter { Property = Button.BorderWidthProperty, Value = 1 },
                        new Setter { Property = Button.BorderColorProperty, Value = Color.FromRgb(230, 230, 230) }
                    }
                } },
                { "OperatorButtonStyle", new Style(typeof(Button)) {
                    Setters = {
                        new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromRgb(255, 128, 0) },
                        new Setter { Property = Button.TextColorProperty, Value = Colors.White }
                    }
                } },
                { "EqualsButtonStyle", new Style(typeof(Button)) {
                    Setters = {
                        new Setter { Property = Button.BackgroundColorProperty, Value = Color.FromRgb(0, 128, 0) },
                        new Setter { Property = Button.TextColorProperty, Value = Colors.White }
                    }
                } }
            };
        }

        private void OnNumberClicked(object sender, EventArgs e)
        {
            var button = (Button)sender;
            if (_isFirstNumber)
            {
                display.Text = button.Text;
                _isFirstNumber = false;
            }
            else
            {
                display.Text += button.Text;
            }
        }

        private void OnOperatorClicked(object sender, EventArgs e)
        {
            var button = (Button)sender;
            if (!_isFirstNumber)
            {
                _currentNumber = double.Parse(display.Text);
                _isFirstNumber = true;
                _currentOperation = button.Text;
            }
        }

        private void OnEqualsClicked(object sender, EventArgs e)
        {
            double secondNumber = double.Parse(display.Text);
            double result = 0;
            switch (_currentOperation)
            {
                case "+":
                    result = _currentNumber + secondNumber;
                    break;
                case "-":
                    result = _currentNumber - secondNumber;
                    break;
                case "*":
                    result = _currentNumber * secondNumber;
                    break;
                case "/":
                    result = _currentNumber / secondNumber;
                    break;
            }
            display.Text = result.ToString();
            _currentOperation = "";
            _isFirstNumber = true;
        }

        private void OnClearClicked(object sender, EventArgs e)
        {
            display.Text = "";
            _currentNumber = 0;
            _currentOperation = "";
            _isFirstNumber = true;
        }
    }
}